	PRINT PUSH,OFF
*******************************************************************************
* StandardLib.Aii
*
* (C)  Copyright Apple Computer, Inc. 1988
* All rights reserved.
*
* by Jim Mensch & Keith Rollin
* 7/11/88
*
* This file contains standard routines used by all the applications on the DTS
* Demo Disk. This file relies on certain data structures being available in a GLOBALS
* data record in the main file.
*
* Files:
*
*
*
* Modification History:
*
* Version 1.0	Jim Mensch and Keith Rollin
*
*      7/11/88
*
*      Initial Release
*
*******************************************************************************

	EJECT
*******************************************************************************
*
InitTools	PROC
*
* Description:	Load and initialize the tools needed. Errors are detected
*	and FatalError is called if any occur. If there aren't any
*	errors, the list of menu templates is read and the menu-
*	bar is created.
*
*
* Inputs:	NONE
*
* Outputs:	NONE
*
* External Refs:
	import FatalError
*
* Entry Points:	NONE
*
*******************************************************************************
	with Globals

;
;   Tool Direct page offsets here
;
QDDPage	equ $0000
EMDPage	equ QDDPage+$0300
CtlDPage	equ EMDPage+$0100
MenuDPage	equ CtlDPage+$0100
LEDPage	equ MenuDPage+$0100
ToolDPSize	equ LEDPage+$0100

	phk	; Save program bank register and
	plb	; load it as the data bank register

	tdc
	sta MyDP	; Save direct register

	_TLStartup	; Start Tool Locator

	pha	; Space for result
	_MMStartup	; Start memory manager
	PullWord MyID	; Save it for later use

	_MTStartup	; Start up Misc Tools

	_IMStartup	; Start integer math toolset

	PushLong #ToolTable	; Pointer to Tool table
	_LoadTools	; Load all RAM based tools
	bcc IT0005	; Carry clear means no error
	brl FatalError	; Tools can't be loaded. Fatal error!

IT0005

; Get memory for Tool Direct pages

	pha	; Room for result
	pha
	PushLong #ToolDPSize; Number of bytes needed
	PushWord MyID	; ID of this application
	PushWord #AttrLocked+attrFixed+attrPage+attrBank
	PushLong #0	; Allocate them in bank 0
	_NewHandle
	bcc IT0010	; Test carry for error
	brl FatalError	; If no memory we got a fatal error!

IT0010	PullLong DPHandle	; Retrieve handle to our DPage area

	lda [DPHandle]	; Dereference the handle to get a ptr
	sta DPPointer	; to our direct page area and save it.

	PushWord DPPointer	; QuickDraw uses 3 pages of Dpage
	PushWord #ScreenMode; Used to set all master SCB's
	PushWord #0	; Zero means use default buf size
	PushWord MyID	; Application ID for allocating data
	_QDStartup	; Start QuickDraw, turn on SHR Screen
	bcc IT0015
	brl FatalError	; If it can't be started then bomb

IT0015
	lda DPPointer	; Create address for Event Mgr DPage by
	clc	; loading in the pointer to the start
	adc #EMDPage	; of DPage and adding Evt Mgr offset.
	pha	; Now push it on the stack.
	PushWord #20	; Size of event queue.
	PushWord #0	; MouseClamp values.
	PushWord #ScreenWidth ; These will clamp mouse to screen
	PushWord #0	; area only.
	PushWord #200
	PushWord MyID
	_EMStartup	; Start the event manager
	bcc IT0020
	brl FatalError	; Event manager is also a must
IT0020
	PushWord MyID
	_WindStartup
	bcc IT0025
	brl FatalError
IT0025
	PushWord MyID
	lda DPPointer
	clc
	adc #CtlDPage
	pha
	_CtlStartup
	bcc IT0030
	brl FatalError
IT0030

	PushWord MyID
	lda DPPointer
	clc
	adc #MenuDPage
	pha
	_MenuStartUp
	bcc IT0035
	brl FatalError
IT0035
	PushWord MyID
	lda DPPointer
	clc
	adc #LEDPage
	pha
	_LEStartUp
	bcc IT0040
	brl FatalError
IT0040
	PushWord MyID
	_DialogStartup
	bcc IT0045
	brl FatalError
IT0045
	_ListStartup

	_ScrapStartup	; No errors possible for this call

	_DeskStartup	; No error possible for this call

	PushLong #0
	_RefreshDeskTop

;
; Create the menus I need
;

	ldx #MenuPtrLen-4	; Get a pointer to the last menu	
loop	phx	; Save menu table index on the stack
	pha	; Push on space for the menu handle
	pha	;   returned by NewMenu
	lda MenuPtr+2,x	; Push on the pointer to the template
	pha	;   to be used by NewMenu when
	lda MenuPtr,x	;   creating the menu
	pha
	_NewMenu	; Create a new menu
	PushWord #0	; Tell InsertMenu where to put it
	_InsertMenu	; Insert it.
	plx	; Get our index back off the stack
	dex	; Point to the next template pointer
	dex
	dex
	dex
	bpl loop	; Go back up if not done yet.

	PushWord #1	; Add NDA's
	_FixAppleMenu

	pha	; Now call CalcMenuSize for all menus
	_FixMenuBar
	PullWord MenuHeight

	_DrawMenuBar	; Finally, draw it.

	RTS

ToolTable	dc.W 7	; Data Block for LoadTools call
	dc.W 14,$0100	; Window Manager
	dc.W 15,$0100	; Menu Manager
	dc.W 16,$0100	; Control Manager
	dc.W 20,$0100	; LineEdit tool set
	dc.W 21,$0100	; Dialog Manager
	dc.W 22,$0100	; Scrap manager
	dc.W 28,$0100	; List Manager

	ENDP

	EJECT
*******************************************************************************
*
FatalError	PROC
*
* Description:	Routine that is called whenever a tool sends back an error
*	that can not be recovered from. This routine prints the
*	error on the screen, waits for a keypress then quits back
*	to whoever started this application up!
*
*
* Inputs:	A = Error number
*
* Outputs:	NONE (program exits)
*
* External Refs:
	import CloseTools
*
* Entry Points:	NONE
*
*******************************************************************************
	with Globals

	pha	; Push the error code
	PushLong #errNumStr	; Address of storage area
	PushWord #4	; Length of string
	_Int2Hex

	_GrafOff	; If QD Started, shut off graphics

	PushLong #ErrStr	; Write error message to the screen
	_WriteCString

	pha	; Space for result
	PushWord #0	; No echo
	_ReadChar	; Wait for a key to be pressed
	pla	; Discard the key

	jsr CloseTools	; Shut down all the tools in case
;		  any got started up

	_Quit QuitParms	; Quit back to where we came from.

	brk $FF	; If the quit fails then just break
	
ErrStr	dc.B 'A fatal error has occured $'
ErrNumStr	dc.B 'xxxx   press any key to exit',0

	ENDP

	EJECT
*******************************************************************************
*
CloseTools	PROC
*
* Description:	Shut down the tools I started.
*
*
* Inputs:	NONE
*
* Outputs:	NONE
*
* External Refs:	NONE
*
* Entry Points:	NONE
*
*******************************************************************************
	with Globals

	_DeskShutDown
	_ScrapShutDown
	_ListShutdown
	_DialogShutDown
	_LEShutDown
	_MenuShutDown
	_CtlShutDown
	_WindShutDown
	_EMShutDown
	_QDShutDown
	_MTShutDown

	PushLong DPHandle	; Dispose of all that DP memory
	_DisposeHandle

	PushWord MyID
	_MMShutDown
	_TLShutDown

	rts
	ENDP

	EJECT
*******************************************************************************
*
doAbout	PROC
*
* Description:	Bring up an Alert Dialog box with our name in it.
*
* Inputs:	NONE
*
* Outputs:	NONE
*
* External Refs:	NONE
*
* Entry Points:	NONE
*
*******************************************************************************
	With Globals

	pha	; space for result
	PushLong #AboutBox	; pointer to alert template
	PushLong #0	; pointer to a filter proc (0=none)
	_NoteAlert
	pla	; get the item hit and dispose

	rts

; About Box alert template

AboutBox	dc.W 30,30,100,590	; for 320 use 30,30,100,290
	dc.W 1	; this is alert # 1
	dc.B $80	; draw for this stage
	dc.B $81	; draw for this stage
	dc.B $82	; draw for this stage
	dc.B $83	; draw for this stage
	dc.L OkButton	; ok button for the	alert
	dc.L AboutTitle	; title of the program
	dc.L AboutAut	; author
	dc.L AboutVers	; version number string
	dc.L 0	; nil to end the list

OKBTitle	dc.B 2,'OK'
OKButton	dc.W 1
	dc.W 50,500,65,550
	dc.W ButtonItem
	dc.L OKBTitle
	dc.W 0
	dc.W 0	; item flag
	dc.L 0	; no color table

AboutTitle	dc.W 2
	dc.W 10,100,20,550
	dc.W StatText+ItemDisable
	dc.L TitleString
	dc.W 0
	dc.W 0	; item flag
	dc.L 0	; no color table

AboutAut	dc.W 3
	dc.W 25,100,35,550
	dc.W StatText+ItemDisable
	dc.L AutString
	dc.W 0
	dc.W 0	; item flag
	dc.L 0	; no color table

AboutVers	dc.W 4
	dc.W 40,100,50,550
	dc.W StatText+ItemDisable
	dc.L VersString
	dc.W 0
	dc.W 0	; item flag
	dc.L 0	; no color table

	ENDP

	PRINT POP